<?php

namespace App\Http\Controllers\AgentAdmin;

use App\Http\Controllers\Controller;
use App\Models\AgentConfig;
use App\Models\AgentUser;
use App\Services\AgentService;
use App\Services\ObsService;

class CommonController extends Controller
{
    /** @var ObsService */
    private $obsService;

    /** @var AgentService */
    private $agentService;

    /**
     * CommonController constructor.
     */
    public function __construct(ObsService $obsService, AgentService $agentService)
    {
        $this->obsService = $obsService;
        $this->agentService = $agentService;
    }

    public function saveAgentSettings()
    {
        /** @var AgentUser $user */
        $user = auth()->user();

        $params = request()->validate([
            AgentConfig::SMS_PROVIDER => 'nullable',
            AgentConfig::ALI_ACCESS_KEY_ID => 'nullable',
            AgentConfig::ALI_ACCESS_KEY_SECRET => 'nullable',
            AgentConfig::SMS_SIGN_NAME => 'nullable',
            AgentConfig::VERIFY_CODE_TEMPLATE => 'nullable',

            AgentConfig::PAY_MERCHANT_NO => 'nullable',
            AgentConfig::PAY_RESALE_AGENT_FEE_RATE => 'nullable',

            AgentConfig::XUNCHENG_OCR_APPKEY => 'nullable',
            AgentConfig::XUNCHENG_REALNAME_APPKEY => 'nullable',
            AgentConfig::XUNCHENG_SMS_APPKEY => 'nullable',
            AgentConfig::XUNCHENG_VERIFY_CODE_TEMPLATE => 'nullable',

            AgentConfig::PRODUCT_PURCHASE_NOTICE => 'nullable',
            AgentConfig::PRODUCT_RESALE_NOTICE => 'nullable',
            AgentConfig::PRODUCT_RESALE_CANCEL_LOCK_TIME => 'nullable',

            AgentConfig::ORDER_LOCK_MINUTES => 'nullable',
            AgentConfig::ORDER_DAILY_CANCEL_LIMIT => 'nullable',
            AgentConfig::RESALE_ORDER_DAILY_CANCEL_LIMIT => 'nullable',

            AgentConfig::WEBSITE_NAME => 'nullable',
            AgentConfig::WEBSITE_LOGO => 'nullable',
            AgentConfig::WEBSITE_ENABLE_RESALE => 'nullable',
            AgentConfig::WEBSITE_ENABLE_EXCHANGE => 'nullable',
            AgentConfig::WEBSITE_ENABLE_CONVERT => 'nullable',
            AgentConfig::WEBSITE_PRIVACY => 'nullable',
            AgentConfig::WEBSITE_AGREEMENT => 'nullable',
            AgentConfig::WEBSITE_ABOUTUS => 'nullable',

            AgentConfig::HEZHI_KEY => 'nullable',
            AgentConfig::HEZHI_URL => 'nullable',
            AgentConfig::DING_PUB_KEY => 'nullable',
            AgentConfig::DING_PRIV_KEY => 'nullable',

            AgentConfig::PAY_CHANNEL => 'nullable',
            AgentConfig::ADAPAY_APP_ID => 'nullable',
            AgentConfig::ADAPAY_DIVIDE_ID => 'nullable',
            AgentConfig::ADAPAY_API_KEY => 'nullable',
            AgentConfig::ADAPAY_RSA_PUBLIC_KEY => 'nullable',
            AgentConfig::ADAPAY_RSA_PRIVATE_KEY => 'nullable',

            AgentConfig::OPEN_WALLET_FEE => 'nullable',

            AgentConfig::DAILY_REGISTER_LIMIT => 'nullable',
            AgentConfig::CHANNEL_REGISTER_ONLY => 'nullable',
        ]);

        $this->agentService->saveAgentSettings($user->agent_id, $params);
    }

    public function listAgentSettings()
    {
        /** @var AgentUser $user */
        $user = auth()->user();

        return $this->agentService->getAgentSettings($user->agent_id, [
            AgentConfig::SMS_PROVIDER,
            AgentConfig::ALI_ACCESS_KEY_ID,
            AgentConfig::ALI_ACCESS_KEY_SECRET,
            AgentConfig::SMS_SIGN_NAME,
            AgentConfig::VERIFY_CODE_TEMPLATE,
            AgentConfig::PAY_MERCHANT_NO,
            AgentConfig::PAY_RESALE_AGENT_FEE_RATE,
            AgentConfig::XUNCHENG_OCR_APPKEY,
            AgentConfig::XUNCHENG_REALNAME_APPKEY,
            AgentConfig::XUNCHENG_SMS_APPKEY,
            AgentConfig::XUNCHENG_VERIFY_CODE_TEMPLATE,
            AgentConfig::PRODUCT_PURCHASE_NOTICE,
            AgentConfig::PRODUCT_RESALE_NOTICE,
            AgentConfig::PRODUCT_RESALE_CANCEL_LOCK_TIME,
            AgentConfig::PAY_SERVICE_FEE_RATE,
            AgentConfig::PAY_RESALE_SERVICE_FEE_RATE,
            AgentConfig::ORDER_LOCK_MINUTES,
            AgentConfig::ORDER_DAILY_CANCEL_LIMIT,
            AgentConfig::RESALE_ORDER_DAILY_CANCEL_LIMIT,

            AgentConfig::WEBSITE_NAME,
            AgentConfig::WEBSITE_LOGO,
            AgentConfig::WEBSITE_ENABLE_RESALE,
            AgentConfig::WEBSITE_ENABLE_CONVERT,
            AgentConfig::WEBSITE_ENABLE_EXCHANGE,
            AgentConfig::WEBSITE_PRIVACY,
            AgentConfig::WEBSITE_AGREEMENT,
            AgentConfig::WEBSITE_ABOUTUS,

            AgentConfig::HEZHI_KEY,
            AgentConfig::HEZHI_URL,
            AgentConfig::DING_PUB_KEY,
            AgentConfig::DING_PRIV_KEY,

            AgentConfig::PAY_CHANNEL,
            AgentConfig::ADAPAY_APP_ID,
            AgentConfig::ADAPAY_DIVIDE_ID,
            AgentConfig::ADAPAY_API_KEY,
            AgentConfig::ADAPAY_RSA_PUBLIC_KEY,
            AgentConfig::ADAPAY_RSA_PRIVATE_KEY,
            AgentConfig::SDPAY_MERCH_NO,
            AgentConfig::SDPAY_RSA_PRIVATE_KEY,

            AgentConfig::OPEN_WALLET_FEE,

            AgentConfig::DAILY_REGISTER_LIMIT,
            AgentConfig::CHANNEL_REGISTER_ONLY,
        ]);
    }

    public function uploadWebsiteLogo()
    {
        request()->validate([
            'file' => ['image', 'mimes:jpg,jpeg,png', 'max:2048', 'dimensions:ratio=1/1'],
        ], [
            'file.max' => '图片尺寸不能超过2M',
            'file.mimes' => '图片格式必须为jpg/jpeg/png',
            'file.dimensions' => '图片长宽比必须为1:1',
        ]);
        $key = 'website_logo_image/' . uniqid();
        $file = request()->file('file');
        $imageUrl = $this->obsService->upload($key, $file);

        return [
            'image_url' => $imageUrl,
        ];
    }

    public function uploadEditorImages()
    {
        $files = request()->allFiles();

        $data = [];
        foreach ($files as $file) {
            request()->validate([
                'file' => ['image', 'mimes:jpg,jpeg,png'],
            ], [
                'file.mimes' => '图片格式必须为jpg/jpeg/png',
            ]);
            $key = 'wangEditor/' . uniqid();
            $imageUrl = $this->obsService->upload($key, $file);

            $data[] = [
                'url' => $imageUrl,
                'href' => '',
                'alt' => '',
            ];
        }

        return [
            'errno' => 0,
            'data' => $data,
        ];
    }

    public function uploadEditorVideo()
    {
        $files = request()->allFiles();

        $data = [];
        foreach ($files as $file) {
            $key = 'wangEditor/' . uniqid();
            $imageUrl = $this->obsService->upload($key, $file);

            $data[] = [
                'url' => $imageUrl,
                'href' => '',
                'alt' => '',
            ];
        }

        return [
            'errno' => 0,
            'data' => $data[0],
        ];
    }
}
